/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 * � Copyright 2013
 * Jana Asmussen, Julian Bau�, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package gui;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import types.GeoTag;
import types.StreamDescriptor;

@ManagedBean(name = "mapBean")
@ViewScoped
public class MapBean implements Serializable {

	private static final long serialVersionUID = 8426678801043265058L;

	private MapModel advancedModel = new DefaultMapModel();
	private GetData data = new GetData();
	private final String positveDot = "http://maps.google.com/mapfiles/ms/micons/green-dot.png";
	private final String negativeDot = "http://maps.google.com/mapfiles/ms/micons/red-dot.png";
	private Marker marker;

	public MapBean() {
		advancedModel = new DefaultMapModel();
	}

	/**
	 * Sets the marker field to the marker was selected on the map.
	 * 
	 * @param event
	 *            a map-overlay event, that triggers this method
	 */
	public void onMarkerSelect(OverlaySelectEvent event) {
		marker = (Marker) event.getOverlay();
	}

	/**
	 * Creates the new map model, that replaces the old one, with the markers
	 * setted on the positions with coordinates from the selected stream
	 * 
	 * @param ae
	 *            ActionEvent, that triggers this method
	 * @param a
	 *            String parameter, that shows the selected stram from the
	 *            stream list
	 */
	public void updateMap(ActionEvent ae, String id) {
		advancedModel = null;
		advancedModel = new DefaultMapModel();

		List<StreamDescriptor> streams = data.getStreams();
		StreamDescriptor stream = data.getStreamDescriptor(id, streams);

		if (stream != null) {
			HashMap<GeoTag,String> valuesPos = data.getGeoTagsWithSentiment(stream, 1);
			for (Entry<GeoTag, String> geoTag : valuesPos.entrySet()) {
				LatLng geoLoc = new LatLng(geoTag.getKey().getLatitude(),
						geoTag.getKey().getLongitude());
				System.out.println(geoTag.getValue());
				Marker marker = new Marker(geoLoc, geoTag.getValue(), geoTag.getValue(), positveDot);
				this.advancedModel.addOverlay(marker);
			}

			HashMap<GeoTag,String> valuesNeg = data.getGeoTagsWithSentiment(stream, -1);
			for (Entry<GeoTag, String> geoTag : valuesNeg.entrySet()) {
				LatLng geoLoc = new LatLng(geoTag.getKey().getLatitude(),
						geoTag.getKey().getLongitude());
				System.out.println(geoTag.getValue());
				Marker marker = new Marker(geoLoc, geoTag.getValue(), geoTag.getValue(), negativeDot);
				this.advancedModel.addOverlay(marker);
			}
		}
	}
	/**
	 * Returns the last selected marker. Returns null if no marker was selected
	 * 
	 * @return the current marker
	 */
	public Marker getMarker() {
		return marker;
	}
	
	/**
	 * Returns model of the map, that is used to represent the map in the
	 * application
	 * 
	 * @return the model, that represents the current Map
	 */
	public MapModel getAdvancedModel() {
		return advancedModel;
	}
	
	/**
	 * Setter of the map module
	 * 
	 * @param advancedModel module of the map
	 */
	public void setAdvancedModel(MapModel advancedModel) {
		this.advancedModel = advancedModel;
	}

	/**
	 * Setting the marker
	 * @param marker marker of the map
	 */
	public void setMarker(Marker marker) {
		this.marker = marker;
	}

}